package persentation.presentDepartmentDean;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import vo.chooseCourseVO.ChooseCourseVO;
import vo.courseVO.CourseVO;
import vo.teachPlanVO.TeachPlanVO;
import vo.userVO.StudentVO;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.courseBl.CourseController;
import businesslogic.teachPlanBl.TeachPlanController;
import businesslogic.userBl.StudentController;

public class AssignCourse extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private DepartmentdeanTabbedPane tabbedPane;
	private JTable table;
	private JTable table_1;
	private String department;
	private JComboBox comboBox,studentBox;
	private JScrollPane jScrollPane,jScrollPane2;
	private Vector data,data2,columnName,columnName2;
	private MyModel model,model_1;
	private TeachPlanController teachControl;
	private ArrayList<StudentVO> studentVOs;
	private JButton btnNewButton;
	private ArrayList<TeachPlanVO> teachPlanVOs;
	
	public AssignCourse(String department) {
		super();
		setSize(900, 690);
		setLayout(null);
		teachControl=new TeachPlanController();
		this.department = department;
		comboBox = new JComboBox();
		comboBox.setBounds(119, 24, 113, 46);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"第一学期",
				"第二学期", "第三学期", "第四学期", "第五学期", "第六学期", "第七学期", "第八学期" }));
		add(comboBox);
		comboBox.addActionListener(this);
		comboBox.setBorder(BorderFactory.createTitledBorder("学期教学计划"));

		columnName=new Vector();
		columnName.add("课程编号");
		columnName.add("课程名称");
		columnName.add("课程性质");
		columnName.add("学分");
		columnName.add("院系");
		
		data=new Vector();
		model = new MyModel(data, columnName);
		table = new JTable(model);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.setFillsViewportHeight(true);
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if ((table.getSelectedRow()!=-1) && (comboBox.getSelectedIndex()!=0)){
//					btnNewButton_1.setEnabled(true);
				}
			}
		});
		jScrollPane = new JScrollPane(table);
		jScrollPane.setBounds(10, 80, 385, 307);
		add(jScrollPane);
		
		studentBox = new JComboBox();
		studentBox.setBounds(539, 24, 113, 46);
		studentBox.setModel(new DefaultComboBoxModel(new String[] {"大一","大二","大三","大四"}));
		add(studentBox);
		studentBox.addActionListener(this);
		studentBox.setBorder(BorderFactory.createTitledBorder("年级"));
		
		columnName2=new Vector();
		columnName2.add("学生编号");
		columnName2.add("学生姓名");
		
		data2=new Vector();
		model = new MyModel(data2, columnName2);
		table_1 = new JTable(model);
		table_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table_1.setFillsViewportHeight(true);
		table_1.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if ((table.getSelectedRow()!=-1) && (comboBox.getSelectedIndex()!=0)){
//					btnNewButton_1.setEnabled(true);
				}
			}
		});
		jScrollPane2 = new JScrollPane(table_1);
		jScrollPane2.setBounds(518, 80, 257, 307);
		add(jScrollPane2);
		
		btnNewButton = new JButton("将课程添加给该年级学生");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnNewButton.setBounds(267, 425, 280, 30);
		add(btnNewButton);
		btnNewButton.addActionListener(this);
		
		updateLeftTable();
		updateRightTable();
	}

	public void setTabbedPane(DepartmentdeanTabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
		
	}

	private void updateLeftTable(){
		try {
			data=new Vector();
			teachPlanVOs=null;
			switch (comboBox.getSelectedIndex()) {
			case 0:
				teachPlanVOs=teachControl.getPlanBySemester("第一学期",department);
				break;
			case 1:
				teachPlanVOs=teachControl.getPlanBySemester("第二学期",department);
				break;
			case 2:
				teachPlanVOs=teachControl.getPlanBySemester("第三学期",department);
				break;
			case 3:
				teachPlanVOs=teachControl.getPlanBySemester("第四学期",department);
				break;
			case 4:
				teachPlanVOs=teachControl.getPlanBySemester("第五学期",department);
				break;
			case 5:
				teachPlanVOs=teachControl.getPlanBySemester("第六学期",department);
				break;
			case 6:
				teachPlanVOs=teachControl.getPlanBySemester("第七学期",department);
				break;
			case 7:
				teachPlanVOs=teachControl.getPlanBySemester("第八学期",department);
				break;
			case 8:
				teachPlanVOs=teachControl.getPlanBySemester("第九学期",department);
				break;
			case 9:
				teachPlanVOs=teachControl.getPlanBySemester("第十学期",department);
				break;
			default:
				break;
			}	
			fillData(teachPlanVOs,data);
			model=new MyModel(data,columnName);
			table.setModel(model);;
//			jScrollPane.setViewportView(table);
		} catch (Exception e) {
			DepartmentdeanExceptionHandler.networkException();
		}
		
	}
	
	private void fillData(ArrayList<TeachPlanVO> teachPlanVOs,Vector myData){
		if (teachPlanVOs==null) return;
		for (TeachPlanVO t:teachPlanVOs){
			Vector row=new Vector();
			row.add(t.getCourseId());
			row.add(t.getCourseName());
			row.add(t.getAttribute());
			row.add(t.getCredit());
			row.add(t.getDepartment());
			myData.add(row);
		}
	}
	
	private void updateRightTable(){
		int grade=studentBox.getSelectedIndex()+1;
		StudentController sc=new StudentController(null);
		data2=new Vector();
		try {
			studentVOs=sc.findStudentByTermAndDepartment(grade, department);
			for (StudentVO s:studentVOs){
				Vector row=new Vector();
				row.add(s.getId());
				row.add(s.getName());
				data2.add(row);
			}
			model_1=new MyModel(data2,columnName2);
			table_1.setModel(model_1);
		} catch (Exception e) {
			e.printStackTrace();
			DepartmentdeanExceptionHandler.networkException();
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()== comboBox){
			updateLeftTable();
		} else if (e.getSource()==studentBox){
			updateRightTable();
		} else if (e.getSource()==btnNewButton){
			for (TeachPlanVO t:teachPlanVOs){
				CourseController courseController=new CourseController(null);
				ChooseCourseController chooseCourseController=new ChooseCourseController(null);
				try {
					CourseVO c=courseController.getCourse(t.getCourseId());
					for (StudentVO s:studentVOs){
						ChooseCourseVO cc=new ChooseCourseVO(s.getId(),s.getName(), c.getCourseId(), c.getCourseName(), 0, c.getCredit(),comboBox.getSelectedIndex()+1, c.getAttribute());
						chooseCourseController.chooseCourseDirect(cc);
					}
				} catch (Exception e2) {
					e2.printStackTrace();
					DepartmentdeanExceptionHandler.networkException();
				}
			}
			JOptionPane.showMessageDialog(null,"指选课程成功！","成功！",JOptionPane.INFORMATION_MESSAGE);
		}
	}
}
